package com.vive.converter;

import org.apache.wicket.request.Url;
import org.apache.wicket.util.convert.ConversionException;
import org.apache.wicket.util.convert.IConverter;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;

public class URLConverter implements IConverter {

    private static final long serivalVersionUID = 1L;
    private Locale locale;

    public void setLocale(Locale locale) {
        this.locale = locale;
        if (null == this.locale) {
            this.locale = Locale.getDefault();
        }
    }

    public Locale getLocale() {
        return this.locale;
    }

    public URLConverter() {
    }

    @Override
    public Object convertToObject(String s, Locale locale) {
        return null;
    }

    @Override
    public String convertToString(Object o, Locale locale) {
        return null;
    }

    public Object convert(Object value,Class c){
        if (value == null) {
            return null;
        }

        if(c == URL.class){
            if(value.getClass()==URL.class){
                return value;
            }

            try {
                return new URL(value.toString());
            } catch (MalformedURLException e) {
                throw new ConversionException( "'" + value + "' is not a valid URL");
            }
        }
        return value.toString();
    }
}
